package com.fezs.message.entity;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.fezs.common.context.BaseContextHandler;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.util.List;

/**
 * @author zhukai
 * @since 2022/8/18
 */
@Getter
@Setter
@ToString
public class MsgEntity implements Serializable {

    public static final String serviceErrorMsgTemplate = "<font color=\"warning\">服务异常提醒:</font>\n" +
        ">**时间:** %s \n" +
        ">**服务:** %s \n" +
        ">**环境:** %s \n" +
        ">**tid:** %s \n" +
        ">**类名:** %s \n" +
        ">**方法:** %s \n" +
        ">**描述:** %s ";

    public static final String webErrorMsgTemplate = "<font color=\"warning\">请求异常提醒:</font>\n" +
        ">**时间:** %s \n" +
        ">**服务:** %s \n" +
        ">**环境:** %s \n" +
        ">**tid:** %s \n" +
        ">**请求:** %s \n" +
        ">**描述:** %s ";

    public static final String jobErrorMsgTemplate = "<font color=\"warning\">定时任务异常提醒:</font>\n" +
        ">**时间:** %s \n" +
        ">**服务:** %s \n" +
        ">**环境:** %s \n" +
        ">**tid:** %s \n" +
        ">**类名:** %s \n" +
        ">**方法:** %s \n" +
        ">**描述:** %s ";

    /**
     * 消息类型
     */
    private String msgtype;

    /**
     * 文本消息
     */
    private TextMsg text;

    /**
     * Markdown消息
     */
    private MarkdownMsg markdown;

    /**
     * image消息
     */
    private ImageMsg image;

    public static MsgEntity buildTextMsg(String content) {
        return buildTextMsg(content, null, null);
    }

    public static MsgEntity buildTextMsg(String content, List<String> userIdList, List<String> mobileList) {
        MsgEntity msgEntity = new MsgEntity();
        msgEntity.setTextMsg(content, userIdList, mobileList);
        return msgEntity;
    }

    public static String getServiceMarkdownMsg(String appName, String profile, String serviceName, String methodName, Throwable e) {
        return String.format(serviceErrorMsgTemplate,
            DateUtil.now(),
            appName,
            profile,
            StrUtil.nullToDefault(BaseContextHandler.getTraceId(), ""),
            serviceName,
            methodName,
            e.getClass().getName() + ": " + e.getMessage() + "\n" + e.getStackTrace()[0].toString()
        );
    }

    public static String getJobMarkdownMsg(String appName, String profile, String serviceName, String methodName, Throwable e) {
        return String.format(jobErrorMsgTemplate,
            DateUtil.now(),
            appName,
            profile,
            StrUtil.nullToDefault(BaseContextHandler.getTraceId(), ""),
            serviceName,
            methodName,
            e.getClass().getName() + ": " + e.getMessage() + "\n" + e.getStackTrace()[0].toString()
        );
    }

    public static String getWebMarkdownMsg(String appName, String profile, String uri, Throwable e) {
        return String.format(webErrorMsgTemplate,
            DateUtil.now(),
            appName,
            profile,
            StrUtil.nullToDefault(BaseContextHandler.getTraceId(), ""),
            uri,
            e.getClass().getName() + ": " + e.getMessage() + "\n" + e.getStackTrace()[0].toString()
        );
    }

    public static MsgEntity buildMarkdownMsg(String content) {
        MsgEntity msgEntity = new MsgEntity();
        msgEntity.setMarkdownMsg(content);
        return msgEntity;
    }

    public static MsgEntity buildImageMsg(String base64, String md5) {
        MsgEntity msgEntity = new MsgEntity();
        msgEntity.setImageMsg(base64, md5);
        return msgEntity;
    }

    public void setTextMsg(String content) {
        this.setTextMsg(content, null, null);
    }

    public void setTextMsg(String content, List<String> userIdList, List<String> mobileList) {
        this.msgtype = "text";
        TextMsg msg = new TextMsg();
        msg.setContent(content);
        msg.setMentioned_list(userIdList);
        msg.setMentioned_mobile_list(mobileList);
        this.text = msg;
    }

    public void setMarkdownMsg(String conten) {
        this.msgtype = "markdown";
        MarkdownMsg msg = new MarkdownMsg();
        msg.setContent(conten);
        this.markdown = msg;
    }

    public void setImageMsg(String base64, String md5) {
        this.msgtype = "image";
        ImageMsg msg = new ImageMsg();
        msg.setBase64(base64);
        msg.setMd5(md5);
        this.image = msg;
    }

    @Getter
    @Setter
    @ToString
    public static class TextMsg {

        /**
         * 文本消息内容
         */
        private String content;

        /**
         * 提醒群中的指定成员(@某个成员)，@all表示提醒所有人，如果开发者获取不到userid，可以使用mentioned_mobile_list
         */
        private List<String> mentioned_list;

        /**
         * 手机号列表，提醒手机号对应的群成员(@某个成员)，@all表示提醒所有人
         */
        private List<String> mentioned_mobile_list;

    }

    @Getter
    @Setter
    @ToString
    public static class MarkdownMsg {

        /**
         * 文本消息内容
         */
        private String content;

    }

    @Getter
    @Setter
    @ToString
    public static class ImageMsg {

        /**
         * 图片内容的base64编码
         */
        private String base64;

        /**
         * 图片内容（base64编码前）的md5值
         */
        private String md5;

    }

}
